package com.hqyj.javaSpringBoot.modules.account.dao;

import com.hqyj.javaSpringBoot.modules.account.entity.Resource;
import com.hqyj.javaSpringBoot.modules.common.entiy.SearchBean;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface ResourceDao {
    /**
     *          /api/resource ---- post  增
     * 			/api/resource ---- put   改
     * 			/api/resource/{id} ---- delete   删
     * 			/api/resource/{id} ---- get      根据id查找
     * 			/api/resources ---- post        条件查找
     */
    /**添加权限*/
    @Insert("insert into account_resource (create_date,perssion) value" +
            " (#{createDate},#{perssion})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void insertResource(Resource resource);

    /**删除权限*/
    @Delete("delete from account_resource where id=#{id}")
    void deleteResourceByReourceId(int id);

    /**修改权限*/
    @Update("update account_resource set perssion=#{perssion} where id=#{id}")
    void updateResourceByReourceId(Resource resource);

    /**根据权限id查询*/
    @Select("select * from account_resource where id=#{id}")
    @Results(id="resourceResult",value = {
            @Result(column = "id",property = "id"),
            @Result(column = "id",property = "roles",javaType = List.class,
                    many=@Many(select = "com.hqyj.javaSpringBoot.modules.account.dao.RoleDao.getRolesByResourceId"))
    })
    Resource getResourceByResourceId(int id);

    @Select("select * from account_resource ar left join account_role_resource arr" +
            " on ar.id=arr.resource_id where arr.role_id =#{roleId} ")
    List<Resource> getResourceByRoleID(int roleId);

    /**根据perssion查询*/
    @Select("select * from account_resource where perssion=#{perssion}")
    List<Resource> getResourceByPerssion(String perssion);



    /**模糊查询*/
    @Select("<script>" +
            " select * from account_resource " +
            " <where>" +
                " <if test='keyWord !=\"null\" and keyWord!=\"\"'>" +
                    "perssion like '%${keyWord}%'" +
                " </if>" +
            " </where>" +
            " <choose>" +
                " <when test='orderBy !=\"null\" and orderBy!=\"\"'>" +
                    " order by ${orderBy} ${direction}" +
                " </when>" +
                "<otherwise>" +
                    "order by id"+
                "</otherwise>" +
            " </choose>" +
            " </script>")
    List<Resource> getResources(SearchBean searchBean);

}
